package com.coconet.gas.rest;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.coconet.gas.biz.*;
import com.coconet.gas.constant.dwEnum;
import com.coconet.gas.dao.AssetsMapper;
import com.coconet.gas.rabbitMq.entity.CheckEntity;
import com.coconet.gas.service.TaskService;
import com.coconet.gas.service.impl.OpenvasTool2;
import com.coconet.gas.service.impl.RabbitMQServiceImpl;
import com.coconet.gas.vo.Assets;
import com.gxa.entity.ResultInfoPage;
import com.coconet.gas.vo.dw.*;
import com.coconet.gas.vo.dw.st.CommonFy;
import com.coconet.gas.vo.request.IpPort    ;
import com.coconet.gas.vo.request.MqTaskRequest;
import com.coconet.gas.vo.request.PortListVo;
import com.coconet.gas.vo.response.PageList;
import com.gxa.entity.*;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import response.BaseResponse;
import response.ObjectRestResponse;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

@RestController
public class TaskRest {
    @Resource
    private IpsRuleBiz ipsRuleBiz;
    @Resource
    private TargetIpBiz targetIpBiz;
    @Resource
    private TaskService taskService;
    @Resource
    private OpenvasTool2 openvasTool;
    @Resource
    private ReportBiz reportBiz;
    @Resource
    private ScanQuartzBiz scBiz;
    @Resource
    private TaskReportRelateBiz trrBiz;
    @Resource
    private MqTaskBiz mqTaskBiz;
    @Resource
    private GroupTaskBiz groupTaskBiz;
    @Resource
    private RabbitMQServiceImpl rabbitMQServiceImpl;
    @Resource
    private OutTimeTaskBiz ottBiz;
    @Resource
    private TaskIpBiz taskIpBiz;
    @Resource
    private AssetsMapper assetsMapper;



    //private OpenvasTool2 openvasTool = new OpenvasTool2();

    //实习test
    @ApiOperation(value = "{测试}显示全部TaskInfoEntity对象",httpMethod = "GET")
    @GetMapping("/getTask")
    public ObjectRestResponse getTask() throws Exception {
        List<TaskInfoEntity> taskInfoEntities = new ArrayList<>();
        List<TaskInfoEntity> tasksList = openvasTool.getTasksList();
        for (TaskInfoEntity taskInfoEntity : tasksList) {
            taskInfoEntities.add(taskInfoEntity);
        }
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "成功啊", taskInfoEntities);
    }

    @ApiOperation(value = "资产漏洞扫描结果分类",httpMethod = "GET")
    @GetMapping("/result")
    public ObjectRestResponse<ResultInfoPage> ResultClassifyPage(
            String startDate,
            String endDate,
            String assetsName,
            String lv,
            String ip,
            @RequestParam(name = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize){
        List<ResultInfoPage> rc = null;
        PageList<ResultInfoPage> pageList = null;
        try {
            //拿到对应条件的查询
            rc = taskService.getResultPageByDateAndLvAndIpAndAssetsName(startDate,endDate,assetsName,lv,ip,pageNum,pageSize);
            //伪造(假)代码 ---start---
            for (ResultInfoPage resultInfoPage : rc) {
                resultInfoPage.setInfluence("无");
                resultInfoPage.setAuthenticationinfo("成功");
                resultInfoPage.setExistExp("无");
            }
            //伪造(假)代码结束 ---end---
            //直接交给分页算法来处理
            List resultInfoPages = getListByPage(rc, pageNum, pageSize);
            //返回集合内容总数 最大页数
            pageList = new PageList<ResultInfoPage>(pageNum, pageSize, rc.size() / pageSize +1, rc.size(), resultInfoPages);
        }catch (Exception e){
            e.printStackTrace();
            return new ObjectRestResponse(dwEnum.FAILEDTASKS.code(),"找不到对应资产漏洞或者请求参数异常,请重试",null);
        }
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(),dwEnum.SUCCEESS.reasonPhraseCN(),pageList);
    }


    //导出漏洞分类结果
    @GetMapping("/resultExport")
    public void test(HttpServletResponse response,
                     String startDate,
                     String endDate,
                     String assetsName,
                     String lv,
                     String ip,
                     @RequestParam(name = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                     @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) throws IOException {
        List<ResultInfoPage> list = taskService.getResultPageByDateAndLvAndIpAndAssetsName(startDate, endDate, assetsName, lv, ip, 0, 9999999);
        ExportParams exportParams = new ExportParams("扫描信息", "报告");
        exportParams.setType(ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, ResultInfoPage.class, list);
        response.setHeader("Content-Disposition","attachment;filename=" + "log.xlsx");
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
//        return list;
    }




    @ApiOperation(value = "获取警告最高top10", httpMethod = "POST")
    @PostMapping("/getWarning")
    public ObjectRestResponse getWarning() throws Exception {
        List<IpResultEntity> ip = taskService.getWarning();
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", ip);
    }


    @ApiOperation(value = "获取低危最高top10", httpMethod = "POST")
    @PostMapping("/getLow")
    public ObjectRestResponse getLow() throws Exception {
        List<IpResultEntity> ip = taskService.getLow();
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", ip);
    }

    @ApiOperation(value = "获取中危最高top10", httpMethod = "POST")
    @PostMapping("/getMid")
    public ObjectRestResponse getMid() throws Exception {
        List<IpResultEntity> ip = taskService.getMid();
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", ip);

    }

    @ApiOperation(value = "获取高危最高top10", httpMethod = "POST")
    @PostMapping("/getHig")
    public ObjectRestResponse getHig() throws Exception {
        List<IpResultEntity> ip = taskService.getHig();
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", ip);

    }

    @ApiOperation(value = "获取安全评分最低top10", httpMethod = "POST")
    @PostMapping("/getScore")
    public ObjectRestResponse getScore() throws Exception {
        List<IpResultEntity> ipResult = taskService.getScore();
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "获取成功", ipResult);
    }


    @ApiOperation(value = "新建端口集合", httpMethod = "POST")
    @PostMapping(value = "/createPortList")
    public BaseResponse createPortList(@RequestBody PortListVo portListVo) throws Exception {
        taskService.createPortList(portListVo);
        return new BaseResponse(dwEnum.SUCCEESS.code(), "新建端口集合成功");
    }

    @ApiOperation(value = "获取端口集合列表", httpMethod = "POST")
    @PostMapping(value = "/getPortList")
    public ObjectRestResponse getPortList(@RequestBody CommonFy cfy) throws Exception {
        List<Map<String, Object>> portList = new ArrayList<>();
        try {
            portList = openvasTool.getPortList();
            List<TargetEntity> targetList = openvasTool.getTargetList();
            for (Map<String, Object> portMap : portList) {
                for (TargetEntity target : targetList) {
                    if (portMap.get("portId").equals(target.getPortId())) {
                        portMap.put("inUse", 1);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List listByPage = getListByPage(portList, cfy.getCurrentPage(), cfy.getPageSize());
        PageList<Map<String, Object>> targetPage = new PageList<Map<String, Object>>(cfy.getCurrentPage(), cfy.getPageSize(), portList.size() / cfy.getPageSize() + 1, listByPage.size(), listByPage);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", targetPage);
    }

    @ApiOperation(value = "批量删除端口集合", httpMethod = "DELETE")
    @DeleteMapping(value = "/deletePortList")
    public BaseResponse deletePortList(@RequestBody List<String> portIdList) throws Exception {
        for (String portId : portIdList) {
            openvasTool.deletePortList(portId);
        }
        return new BaseResponse(dwEnum.SUCCEESS.code(), "删除端口集合成功");
    }


    @ApiOperation(value = "新建target", httpMethod = "POST")
    @PostMapping(value = "/createTarget")
    public BaseResponse createTarget(@RequestBody TargetEntity target) throws Exception {
        taskService.createTarget(target);
        return new BaseResponse(dwEnum.SUCCEESS.code(), "新建扫描目标成功");
    }


    @ApiOperation(value = "获取Target列表", httpMethod = "POST")
    @PostMapping(value = "/getTargetList")
    public ObjectRestResponse getTargetList(@RequestBody CommonFy cfy) {
        List<TargetEntity> targetList = new ArrayList<>();
        try {
            targetList = openvasTool.getTargetList();
            List<TaskInfoEntity> taskList = openvasTool.getTasksList();
            for (TargetEntity target : targetList) {
                for (TaskInfoEntity task : taskList) {
                    if (task.getTargetId().equals(target.getUuid())) {
                        target.setInUse(1);
                    }
                }
                List<Map<String, Object>> portList = openvasTool.getPortList();
                for (Map<String, Object> port : portList) {
                    if (target.getPortId().equals(port.get("portId"))) {
                        target.setPortName(port.get("name").toString());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //PageHelper.startPage(cfy.getCurrentPage(), cfy.getPageSize());
        //PageInfo<TargetEntity> pageInfo = new PageInfo<TargetEntity>(targetList);

        List listByPage = getListByPage(targetList, cfy.getCurrentPage(), cfy.getPageSize());
        PageList<TargetEntity> targetPage = new PageList<TargetEntity>(cfy.getCurrentPage(), cfy.getPageSize(), targetList.size() / cfy.getPageSize() + 1, listByPage.size(), listByPage);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", targetPage);
    }

    @ApiOperation(value = "批量删除target", httpMethod = "DELETE")
    @DeleteMapping(value = "/deleteTarget")
    public BaseResponse deleteTarget(@RequestBody List<String> targetIdList) throws Exception {
        for (String targetId : targetIdList) {
            openvasTool.deleteTarget(targetId);
        }
        return new BaseResponse(dwEnum.SUCCEESS.code(), "删除扫描目标成功");
    }


    @ApiOperation(value = "新建任务", httpMethod = "POST")
    @PostMapping(value = "/createTask")
    public BaseResponse createTask(@RequestBody TaskEntity taskEntity) throws Exception {
        taskService.createTask(taskEntity);
        return new BaseResponse(dwEnum.SUCCEESS.code(), "新建任务成功");
    }

    @ApiOperation(value = "获取Task列表中未完成任务个数", httpMethod = "POST")
    @PostMapping(value = "/getTasksNum")
    public ObjectRestResponse getTasksNum() {

        int tasksNum = 0;
        try {
            tasksNum = openvasTool.getTasksNum();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", tasksNum);
    }

    @ApiOperation(value = "获取Task列表", httpMethod = "POST")
    @PostMapping(value = "/getTasksList")
    public ObjectRestResponse getTasksList(@RequestBody CommonFy cfy) {
        List<TaskInfoEntity> taskList = getTaskListCommon();
        List listByPage = getListByPage(taskList, cfy.getCurrentPage(), cfy.getPageSize());
        PageList<TaskInfoEntity> taskPage = new PageList<TaskInfoEntity>(cfy.getCurrentPage(), cfy.getPageSize(), taskList.size() / cfy.getPageSize() + 1, listByPage.size(), listByPage);
        //PageHelper.startPage(cfy.getCurrentPage(), cfy.getPageSize());
        //PageInfo<TaskInfoEntity> pageInfo = new PageInfo<TaskInfoEntity>(taskList);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", taskPage);
    }

    @ApiOperation(value = "批量删除任务", httpMethod = "DELETE")
    @DeleteMapping(value = "/deleteTask")
    public BaseResponse deleteTask(@RequestBody List<String> taskIdList) {
        for (String taskId : taskIdList) {
            taskService.deleteTask(taskId);
        }
        return new BaseResponse(dwEnum.SUCCEESS.code(), "删除任务成功");
    }

    @ApiOperation(value = "获取Config列表", httpMethod = "GET")
    @GetMapping(value = "/getConfigList")
    public ObjectRestResponse getConfigList() {
        List<ConfigEntity> configList = new ArrayList<>();
        try {
            configList = openvasTool.getConfigList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", configList);
    }


    @ApiOperation(value = "开始任务", httpMethod = "GET")
    @GetMapping(value = "/startTask/{taskId}")
    public ObjectRestResponse startTask(@PathVariable String taskId) {
        taskService.startTask(taskId);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", "开始扫描任务成功");
    }

    @ApiOperation(value = "停止任务", httpMethod = "GET")
    @GetMapping(value = "/stopTask/{taskId}")
    public ObjectRestResponse stopTask(@PathVariable String taskId) {
        try {
            openvasTool.stopTask(taskId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", "停止扫描任务成功");
    }


    @ApiOperation(value = "获取报告结果", httpMethod = "GET")
    @GetMapping(value = "/getReport/{reportId}")
    public ObjectRestResponse getReport(@PathVariable String reportId) throws Exception {
       /* Map<String, Object> resultMap = openvasTool.getReport(reportId);
        List<Map<String, Object>> results = (List<Map<String, Object>>) resultMap.get("results");
        List<String> resultIdList = new ArrayList<>();
        for (Map<String, Object> result : results) {
            String resultId = (String) result.get("resultId");
            resultIdList.add(resultId);
        }*/
        List<Report> reportList = taskService.getReportByReportId(reportId);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", reportList);
    }

    @ApiOperation(value = "通过resultId获取结果", httpMethod = "GET")
    @GetMapping(value = "/getReportByReportId/{resultId}")
    public ObjectRestResponse getReportByReportId(@PathVariable String resultId) throws Exception {
        Map<String, Object> reportResult = openvasTool.getReportResult(resultId);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", reportResult);
    }

    @ApiOperation(value = "通过任务id获取报告结果", httpMethod = "GET")
    @GetMapping(value = "/getReportByTaskID/{taskId}")
    public ObjectRestResponse getReportByTaskId(@PathVariable String taskId) throws Exception {
        List<Report> reportList = taskService.getReportByTaskId(taskId);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", reportList);
    }


    @ApiOperation(value = "自动创建扫描任务", httpMethod = "POST")
    @PostMapping(value = "/createTaskAuto")
    public BaseResponse createTaskAuto(@RequestBody IpPort ipPort) throws Exception {
        TargetEntity target = new TargetEntity();
        target.setHosts(ipPort.getIp());
        target.setName(ipPort.getIp());
        target.setPortId("5f2029f6-56fe-11e1-bb94-406186ea4fc5");
        createTarget(target);
        List<TargetEntity> targetList = openvasTool.getTargetList();
        String targetId = null;
        for (TargetEntity targetEntity : targetList) {
            if (targetEntity.getName().equals(ipPort.getIp())) {
                targetId = targetEntity.getUuid();
            }
        }
        TaskEntity task = new TaskEntity();
        task.setTargetId(targetId);
        task.setName(ipPort.getIp());
        createTask(task);
        return new BaseResponse(dwEnum.SUCCEESS.code(), "新建任务成功");
    }

    @ApiOperation(value = "通过任务id获取报告id", httpMethod = "GET")
    @GetMapping(value = "/getReportIdByTaskID/{taskId}")
    public ObjectRestResponse getReportIdByTaskId(@PathVariable String taskId) throws Exception {
        TaskReportRelate trr = new TaskReportRelate();
        trr.setTaskId(taskId);
        List<TaskReportRelate> trrList = trrBiz.selectList(trr);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", trrList);
    }


    /*@ApiOperation(value = "通过host获取报告", httpMethod = "POST")
    @PostMapping(value = "/getReportByHost")*/
    @ApiOperation(value = "通过host获取报告", httpMethod = "GET")
    @GetMapping(value = "/getReportByHost/{host}")
    //public ObjectRestResponse getReportByHost(@RequestBody IpPort ipPort) throws Exception {
    public ObjectRestResponse getReportByHost(@PathVariable String host) throws Exception {
        List<Report> reportList = taskService.getReportByHost(host);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", reportList);
    }

    @ApiOperation(value = "通过Ip获取targetId", httpMethod = "GET")
    @GetMapping(value = "/getTargetIdByIp/{ip}")
    public ObjectRestResponse getTargetIdByIp(@PathVariable String ip) throws Exception {
        List<TargetEntity> targetList = openvasTool.getTargetList();
        String targetId = null;
        for (TargetEntity target : targetList) {
            if (target.getHosts().equals(ip)) {
                targetId = target.getUuid();
                break;
            }
        }
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", targetId);
    }

    @ApiOperation(value = "通过Ip获取报告Id", httpMethod = "GET")
    @GetMapping(value = "/getReportIdByIp/{ip}")
    public ObjectRestResponse getReportIdByIp(@PathVariable String ip) throws Exception {
        TaskReportRelate trr = new TaskReportRelate();
        trr.setHost(ip);
        List<TaskReportRelate> trrLi = trrBiz.selectList(trr);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", trrLi);
    }

    @ApiOperation(value = "获取有报告的ip", httpMethod = "GET")
    @GetMapping(value = "/getHasReportIp/{currentPage}/{pageSize}")
    public ObjectRestResponse getHasReportIp(@PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        List<TaskReportRelate> trrLi = trrBiz.selectListAll();
        List<String> ipList = new ArrayList<>();
        List<String> doneTaskList = new ArrayList<>();
        List<TaskInfoEntity> taskList = null;
        try {
            taskList = openvasTool.getTasksList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (TaskInfoEntity task : taskList) {
            String status = task.getStatus();
            if (status.equals("已完成")) {
                doneTaskList.add(task.getTaskId());
            }
        }
        for (TaskReportRelate trr : trrLi) {
            if (!ipList.contains(trr.getHost()) && doneTaskList.contains(trr.getTaskId())) {
                ipList.add(trr.getHost());
            }
        }
        List listByPage = getListByPage(ipList, currentPage, pageSize);
        PageList<TaskInfoEntity> ipPage = new PageList<TaskInfoEntity>(currentPage, pageSize, ipList.size() / pageSize + 1, listByPage.size(), listByPage);

        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", ipPage);
    }

    @ApiOperation(value = "批量删除任务", httpMethod = "DELETE")
    @DeleteMapping(value = "/deleteAllByIp")
    public BaseResponse deleteAllByIp(@RequestBody List<String> ipList) {
        for (String ip : ipList) {
            taskService.deleteAllByIp(ip);
        }
        return new BaseResponse(dwEnum.SUCCEESS.code(), "删除成功");
    }


    public List getListByPage(List list, int page, int pageSize) {
        if (list.size() > 0) {
            int firstIndex = (page - 1) * pageSize;
            int lastIndex = page * pageSize;
            int actualLastIndex = 0;
            if (list.size() >= lastIndex) {
                actualLastIndex = lastIndex;
            } else {
                actualLastIndex = list.size();
            }
            return list.subList(firstIndex, actualLastIndex);
        }
        return list;
    }

    //将信息添加到rabbitmp中
    @ResponseBody
    @PostMapping(value = "/addTargetAndTasksRabbitmq")
    public BaseResponse addTargetAndTasksRabbitmq(@RequestBody CheckEntity checkEntity) {
        GroupTask groupTask = new GroupTask();
        groupTask.setTaskName(checkEntity.getName());
        List<GroupTask> groupTaskList = groupTaskBiz.selectList(groupTask);
        if (groupTaskList.size() != 0) {
            return new BaseResponse(dwEnum.FAILURE.code(), "该任务名称已存在,请确认后重新添加");
        }
        taskService.addTargetAndTasksRabbitmq(checkEntity);
        return new BaseResponse(dwEnum.SUCCEESS.code(), "添加成功");
    }

    @ApiOperation(value = "获取任务组", httpMethod = "GET")
    @GetMapping(value = "/getGroupTask/{currentPage}/{pageSize}")
    public ObjectRestResponse getGroupTask(@PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        List<GroupTask> groupTaskList = groupTaskBiz.selectListAll();
        List listByPage = getListByPage(groupTaskList, currentPage, pageSize);
        PageList<GroupTask> groupTaskPage = new PageList<GroupTask>(currentPage, pageSize, groupTaskList.size() / pageSize + 1, listByPage.size(), listByPage);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", groupTaskPage);
    }

    @ApiOperation(value = "获取通过组添加任务记录", httpMethod = "POST")
    @PostMapping(value = "/getMqTask")
    public ObjectRestResponse getMqTask(@RequestBody MqTaskRequest mqTaskRequest) {
        MqTask mqTask = new MqTask();
        mqTask.setGroupTaskId(mqTaskRequest.getGroupId());
        List<MqTask> mqTaskList = mqTaskBiz.selectList(mqTask);
        List listByPage = getListByPage(mqTaskList, mqTaskRequest.getCurrentPage(), mqTaskRequest.getPageSize());
        PageList<MqTask> mqTaskPage = new PageList<MqTask>(mqTaskRequest.getCurrentPage(), mqTaskRequest.getPageSize(), mqTaskList.size() / mqTaskRequest.getPageSize() + 1, listByPage.size(), listByPage);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", mqTaskPage);
    }

    @ApiOperation(value = "获取通过组添加的正在执行/执行完成任务", httpMethod = "POST")
    @PostMapping(value = "/getMqTaskAdded")
    public ObjectRestResponse getMqTaskAdded(@RequestBody MqTaskRequest mqTaskRequest) {
        List<TaskInfoEntity> taskList = getTaskListCommon();
        List<TaskInfoEntity> taskListAdded = new ArrayList<>();
        for (TaskInfoEntity task : taskList) {
            if (task.getName().startsWith(mqTaskRequest.getTaskName())) {
                taskListAdded.add(task);
            }
        }
        List listByPage = getListByPage(taskListAdded, mqTaskRequest.getCurrentPage(), mqTaskRequest.getPageSize());
        PageList<TaskInfoEntity> mqTaskPage = new PageList<TaskInfoEntity>(mqTaskRequest.getCurrentPage(), mqTaskRequest.getPageSize(), taskListAdded.size() / mqTaskRequest.getPageSize() + 1, listByPage.size(), listByPage);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", mqTaskPage);
    }

    @ApiOperation(value = "获取通过ip添加的任务", httpMethod = "GET")
    @GetMapping(value = "/getIpTask/{currentPage}/{pageSize}")
    public ObjectRestResponse getIpTask(@PathVariable Integer currentPage, @PathVariable Integer pageSize) {
        List<TaskInfoEntity> taskList = getTaskListCommon();
        List<TaskInfoEntity> ipTaskList = new ArrayList<>();

        List<TaskIp> taskIpList = taskIpBiz.selectListAll();
        for (TaskIp taskIp : taskIpList) {
            for (TaskInfoEntity task : taskList) {
                if (task.getTaskId().equals(taskIp.getTaskId())) {
                    ipTaskList.add(task);
                }
            }
        }
        List listByPage = getListByPage(ipTaskList, currentPage, pageSize);
        PageList<TaskInfoEntity> groupTaskPage = new PageList<TaskInfoEntity>(currentPage, pageSize, ipTaskList.size() / pageSize + 1, listByPage.size(), listByPage);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", groupTaskPage);
    }

    @ApiOperation(value = "批量删除组任务", httpMethod = "POST")
    @PostMapping(value = "/deleteGroupTask")
    public BaseResponse deleteMqTask(@RequestBody List<Integer> groupIdList) {
        for (Integer groupId : groupIdList) {
            groupTaskBiz.deleteById(groupId);
            MqTask mqTask = new MqTask();
            mqTask.setGroupTaskId(groupId);
            List<MqTask> mqTaskList = mqTaskBiz.selectList(mqTask);
            String taskName = mqTaskList.get(0).getTaskName();
            List<TaskInfoEntity> taskList = getTaskListCommon();
            List<String> taskIdList = new ArrayList<>();
            for (TaskInfoEntity task : taskList) {
                if (task.getName().contains(taskName)) {
                    taskIdList.add(task.getTaskId());
                }
            }
            deleteTask(taskIdList);
            mqTaskBiz.delete(mqTask);
        }
        return new BaseResponse(dwEnum.SUCCEESS.code(), "删除任务成功");
    }


    @ApiOperation(value = "获取超时任务列表", httpMethod = "POST")
    @PostMapping(value = "/getOutTimeTask")
    public ObjectRestResponse getOutTimeTask(@RequestBody MqTaskRequest mqTaskRequest) {
        List<OutTimeTask> outTimeTaskList = ottBiz.selectListAll();
        List<OutTimeTask> ottList = new ArrayList<>();
        for (OutTimeTask outTimeTask : outTimeTaskList) {
            if (outTimeTask.getTaskName().contains(mqTaskRequest.getTaskName())) {
                ottList.add(outTimeTask);
            }
        }

        List listByPage = getListByPage(ottList, mqTaskRequest.getCurrentPage(), mqTaskRequest.getPageSize());
        PageList<OutTimeTask> ottPage = new PageList<OutTimeTask>(mqTaskRequest.getCurrentPage(), mqTaskRequest.getPageSize(), ottList.size() / mqTaskRequest.getPageSize() + 1, listByPage.size(), listByPage);

        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "", ottPage);
    }


    public List<TaskInfoEntity> getTaskListCommon() {
        List<TaskInfoEntity> taskList = new ArrayList<>();
        try {
            taskList = openvasTool.getTasksList();

            for (TaskInfoEntity task : taskList) {
                /*List<TargetEntity> targetList = openvasTool.getTargetList();
                for (TargetEntity target : targetList) {
                    if (task.getTargetId().equals(target.getUuid())) {
                        task.setHost(target.getHosts());
                        break;
                    }
                }*/

                ScanQuartz sq = scBiz.selectById(task.getTaskId());
                if (sq != null) {
                    task.setType(1);
                    task.setCron(sq.getCron());
                    task.setOnOff(sq.getOnOff());
                } else {
                    task.setType(0);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*for (TaskInfoEntity task : taskList) {
            String checkTime = task.getCheckTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date parse = sdf.parse(checkTime);
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(parse);
                rightNow.add(Calendar.HOUR_OF_DAY, 16);
                String format = sdf.format(rightNow.getTime());
                task.setCheckTime(format);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }*/
        Collections.sort(taskList, (t1, t2) -> t2.getCheckTime().compareTo(t1.getCheckTime()));
        return taskList;
    }

}
